package com.miguo.view;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.miguo.ui.R;

import java.util.Calendar;

/**
 * calendar adapter.
 *
 * @author Sazonov-adm
 */
public class CalendarAdapter extends BaseAdapter {

    /**
     * references to our items.
     */
    private DayCell[] days;

    //当前日期
    private int mCurrentDate;

    /**
     * context.
     */
    private Context mContext;

    /**
     * init month.
     */
    private java.util.Calendar mCurrentMonth;
    private int index;// number=1��DayCellԪ�ض�Ӧ�ļ����±�

    ViewGroup mParent;

    /**
     * @param context       context.
     * @param monthCalendar current month.
     */
    protected CalendarAdapter(Context context, Calendar monthCalendar) {
        mCurrentDate = monthCalendar.get(Calendar.DAY_OF_MONTH);
        mCurrentMonth = (Calendar) monthCalendar.clone();
        mContext = context;
        mCurrentMonth.set(Calendar.DAY_OF_MONTH, 1);

        refreshDays();
    }

    /**
     * @param month current month.
     */
    public final void setMonth(Calendar month) {
        mCurrentMonth = (Calendar) month.clone();
    }

    public int getCount() {
        return days.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        DayCell dayCell = days[position];
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.calendar_item, null);
        }
        TextView tvDate = (TextView) convertView.findViewById(R.id.date);
        boolean isCurrentMonth = dayCell.mDate.get(Calendar.MONTH) == mCurrentMonth
                .get(Calendar.MONTH);
        if (!isCurrentMonth) {
            tvDate.setVisibility(View.INVISIBLE);
        } else {
            tvDate.setVisibility(View.VISIBLE);
            if (dayCell.mDate.get(Calendar.DAY_OF_MONTH) == 1) {
                index = position;// 记录当月1号的位置
            }
        }

        if (dayCell.mDate.get(Calendar.DAY_OF_MONTH) <= mCurrentDate) {
            tvDate.setTextColor(Color.parseColor("#e85148"));
        }
        tvDate.setText(dayCell.mDate.get(Calendar.DAY_OF_MONTH) + "");
        return convertView;
    }

    /**
     * refresh.
     */
    public final void refreshDays() {
        Calendar currentDate = DateHelper.fromDateToCalendar(DateHelper
                .clearTime(((Calendar) mCurrentMonth.clone()).getTime()));
        currentDate.set(Calendar.DAY_OF_MONTH, 1);

        int firstDay = currentDate.get(Calendar.DAY_OF_WEEK);
        int dayShift;
        if (DateHelper.isMondayFirst()) {
            if (firstDay == Calendar.SUNDAY) {
                dayShift = 6;
            } else {
                dayShift = firstDay - 2;
            }
        } else {
            dayShift = firstDay - 1;
        }
        currentDate.add(Calendar.DATE, -dayShift);
        days = new DayCell[42];
        for (int i = 0; i < days.length; i++) {
            days[i] = new DayCell(currentDate);
            DateHelper.increment(currentDate);
        }
    }

    /**
     * @author Sazonov-adm
     */
    public class DayCell {
        /**
         * ����
         */
        public int number;
        /**
         * date.
         */
        public Calendar mDate;
        /**
         * �Ƿ�ǩ��
         */
        public boolean isQianDao = false;

        /**
         * @param currentDate date.
         */

        public DayCell(Calendar currentDate) {
            mDate = (Calendar) currentDate.clone();
        }

        @Override
        public final int hashCode() {
            return super.hashCode();
        }

        @Override
        public final boolean equals(Object other) {
            // Not strictly necessary, but often a good optimization
            if (this == other) {
                return true;
            }
            if (!(other instanceof DayCell)) {
                return false;
            }
            DayCell otherA = (DayCell) other;
            return (mDate.equals(otherA.mDate))
                    && ((mDate == null) ? otherA.mDate == null : mDate
                    .equals(otherA.mDate));
        }
    }

//	public final void updateView(int index) {
//		days[index].tvDate
//				.setBackgroundResource(R.drawable.calendar_item_background_today);
//	}

    public final int getIndext() {
        return index;
    }

}